﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZJFramework10.Extension;
namespace TrainingsApp.IPADApp.Master {
    public partial class TeacherManager : PageBase {
        public int SchoolId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("sid");
            }
        }

        protected void Page_Load(object sender, EventArgs e) {


            cbxCourse.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            cbxCourse.ValueField = "Idx";
            cbxCourse.ValueType = typeof(int);
            cbxCourse.TextField = "CourseName";
            cbxCourse.DataBind();
            cbxCourse.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("-- 所有学科 --", -1));

            if (!this.IsPostBack) {
                cbxCourse.SelectedIndex = 0;
            }

            Databinds();
        }


        protected void Databinds() {
            //Stopwatch watch = new Stopwatch();
            //watch.Start();

            System.Collections.Specialized.StringDictionary queryWheres = new System.Collections.Specialized.StringDictionary();
            if (SchoolId > 0) {

                queryWheres.Add("SchoolId", SchoolId.ToString());
            }
            if (this.cbxCourse.SelectedIndex > 0) {
                int cVal = this.cbxCourse.Value.AsInt();

                queryWheres.Add("TS_User_Teacher.CourseId", cVal.ToString());
            }

            if (!String.IsNullOrEmpty(this.txtEmail.Text)) {
                queryWheres.Add("Email", string.Format("%{0}%", this.txtEmail.Text));
            }

            if (!String.IsNullOrEmpty(this.txtIdCard.Text)) {
                queryWheres.Add("IdentityCard", string.Format("%{0}%", this.txtIdCard.Text));
            }

            if (!String.IsNullOrEmpty(this.txtMobile.Text)) {
                queryWheres.Add("Mobile", string.Format("%{0}%", this.txtMobile.Text));
            }
            if (!String.IsNullOrEmpty(this.txtSchoolName.Text)) {
                queryWheres.Add("SchoolName", string.Format("%{0}%", this.txtSchoolName.Text));
            }
            if (!String.IsNullOrEmpty(this.txtUserName.Text)) {
                queryWheres.Add("UserName", string.Format("%{0}%", this.txtUserName.Text));
            }




            var collection = TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Children_ListToGridByPaging(this.aspDataSource.SettingsPager.PageSize, this.aspDataSource.PageIndex + 1, queryWheres);
            this.aspDataSource.DataSource = GetAllPager(aspDataSource.PageIndex, this.aspDataSource.SettingsPager.PageSize, collection.TS_User_Teacher_List, collection.TotalCount);
            this.aspDataSource.DataBind();
            //watch.Stop();
            //Response.Write(watch.ElapsedMilliseconds);
        }

        protected void aspDataSource_PageIndexChanged(object sender, EventArgs e) {
            Databinds();
        }


        protected void aspDataSource_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e) {
            long key = Convert.ToInt64(this.aspDataSource.GetRowValues(e.VisibleIndex, "UserId"));
            if (e.ButtonID == "btnNew") {
                Response.Redirect("TeacherEdit.aspx");
            }
            else if (e.ButtonID == "btnEdit") {
                Response.Redirect("TeacherEdit.aspx?uid=" + key);
            }

        }
    }
}